<?php
namespace app\mini_api\controller;

class Message extends Permission
{
    public function _initialize()
    {
        parent::_initialize();
        parent::check();
    }

    public function getList()
    {
        $page = $this->input_data['page'] ? $this->input_data['page'] : 1;
        $pageSize = $this->input_data['pageSize'] ? $this->input_data['pageSize'] : 15;
        $map = [];
        $map['driver_id'] = $this->user['id'];
        $map['read_time'] = $this->input_data['read'] == 1 ? ['>',0] : 0;
        $total = db('DriverMessage')->where($map)->count();
        $row = db('DriverMessage')->alias('a')->join('message b','a.message_id = b.id','LEFT')->field('a.*,b.content')->order('id DESC')->where($map)->page($page,$pageSize)->select();
        $list = [];

        foreach ($row as $v) {
            $v['sendTime'] = date('Y-m-d H:i:s',$v['send_time']);
            $list[] = $v;
        }
        
        
        $data = array(
            'total' => $total,
            'list' => $list     
        );
        $this->resSuc($data);
    }
    public function getById()
    {
        $map['a.id'] = $this->input_data['id'];
        $detail = db('DriverMessage')->alias('a')->join('message b','a.message_id = b.id','LEFT')->field('a.*,b.content')->order('id DESC')->where($map)->find();
        $list = [];

        $detail['sendTime'] = date('Y-m-d H:i:s',$detail['send_time']);

        $this->_read();

        $this->resSuc($detail);
    }

    private function _read()
    {
        $map['id'] = $this->input_data['id'];
        $data = [
            'read_time' => time()
        ];
        db('DriverMessage')->where($map)->update($data);
    }
    public function read()
    {
        for ($i=0; $i < 10; $i++) { 
            $data = [
                'type' => '小程序信息',
                'driver_id' => 4,
                'message_id' => 5
            ];
            db('DriverMessage')->insert($data);
        }
        
    }



}
